<template>
  <div class="main-content">
    <div style="width: 70%; background-color: white; margin: 30px auto; border-radius: 20px">
      <div style="padding-bottom: 10px">
        <div style="font-size: 18px; color: #000000FF; line-height: 80px; border-bottom: #cccccc 1px solid;">
          <div style="margin-left: 20px">全部收藏（{{ collectData.length }}件）</div>
        </div>
        <div style="margin: 20px 0; padding: 0 50px">
          <div class="table">
            <el-table :data="collectData" strip>
              <el-table-column  label="商品图片" width="120px">
                <template v-slot="scope">
                  <a @click="$router.push('/front/detail?id=' + scope.row.goodsId)"><el-image  style="width: 80px; height: 60px; border-radius: 3px" v-if="scope.row.goodsImg"
                    :src="scope.row.goodsImg" :preview-src-list="[scope.row.goodsImg]"></el-image></a>
                </template>
              </el-table-column>
              <el-table-column prop="goodsName" label="商品名称" width="350px" ></el-table-column>
              <el-table-column prop="businessName" label="店铺名称">
                <template slot-scope="scope">
                  <a   v-if="scope.row.businessId"
                       @click="$router.push('/front/business?id=' + scope.row.businessId)">
                    {{ scope.row.businessName }}
                  </a>


                </template>
              </el-table-column>
              <el-table-column prop="price" label="商品价格"></el-table-column>
              <el-table-column label="操作" align="center" width="180">
                <template v-slot="scope">
                  <el-button size="mini" type="danger" plain @click="del(scope.row.id)">移除收藏</el-button>
                </template>
              </el-table-column>
            </el-table>

            <div class="pagination" style="margin-top: 20px">
              <el-pagination
                  background
                  @current-change="handleCurrentChange"
                  :current-page="current"
                  :page-sizes="[5, 10, 20]"
                  :page-size="pageSize"
                  layout="total, prev, pager, next"
                  :total="total">
              </el-pagination>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>

export default {

  data() {
    return {
      user: JSON.parse(localStorage.getItem('xm-user') || '{}'),
      collectData: [],
      current: 1,   // 当前的页码
      pageSize: 10,  // 每页显示的个数
      total: 0,
      goodsId:0,
    }
  },
  mounted() {
    this.loadCollect(1)
  },
  // methods：本页面所有的点击事件或者其他函数定义区
  methods: {
    loadCollect(current) {
      if (current) this.current = current
      this.$request.post('/collect/selectPage', {

          current: Number(this.current),
          pageSize: Number(this.pageSize),

      }).then(res => {
        if (res.code === 0) {
          this.collectData = res.data?.records
          this.total =Number(res.data?.total)
          this.goodsId = res.data?.records.goodsId
          this.businessId = res.data?.records.businessId
          this.businessName = res.data?.records.businessName
        } else {
          this.$message.error(res.message)
        }
      })
    },

    del(id) {
      this.$request.delete('/collect/delete/' + id).then(res => {
        if (res.code === 0) {
          this.$message.success('移除成功')
          this.loadCollect(1)
        } else {
          this.$message.error(res.message)
        }
      })
    },
    handleCurrentChange(current) {
      this.loadCollect(current)
    }
  }
}
</script>
